/*
 * Tabella.java
 *
 * Created on 3 settembre 2008, 11.53
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package DBObjects;

import java.util.ArrayList;

/**
 *
 * @author Adriano Aristarco
 */
public class dbTable implements dbObject {
    
    private String tableName;
    private ArrayList<dbColumn> colonne = new ArrayList<dbColumn>();
    private ArrayList<dbForeignKey> foreignKey = new ArrayList<dbForeignKey>();
    private dbPrimaryKey primaryKey;
    
    /** Creates a new instance of Tabella */
    public dbTable(String tableName) {
        this.tableName = tableName;
    }

    /* TO DO creare un costruttore che accetti un numero n di colonne e le
     * inserisca in automatico
     */
    
    public dbColumn addColumn(String columnName, String columnType)
    {
        dbColumn c = new dbColumn(columnName, columnType);
        this.addColumn(c);
        return c;
    }
    
    public void addColumn(dbColumn c)
    {
        getColonne().add(c);
    }

    /* rimuove la prima colonna con quel nome */
    public void dropColumn(String columnName)
    {
         for ( dbColumn col : this.getColonne())
           if(col.getColumnName().equals(columnName))
           {
               this.getColonne().remove(col);
               return;
           }
    }
   
    public void dropColumn(dbColumn c)
    {
        getColonne().remove(c);
    }

    public boolean hasColumn(dbColumn c)
    {
        return getColonne().contains((dbColumn) c);
    }

    @Override
    public int getType() {
        return dbObject.TABLE;
    }

    /**
     * @return the tableName
     */
    public String getTableName() {
        return tableName;
    }

    /**
     * @return the colonne
     */
    public ArrayList<dbColumn> getColonne() {
        return colonne;
    }

    /**
     * @return the primaryKey
     */
    public dbPrimaryKey getPrimaryKey() {
        return primaryKey;
    }

    /**
     * @param primaryKey the primaryKey to set
     */
    public void setPrimaryKey(dbPrimaryKey primaryKey) {
        this.primaryKey = primaryKey;
    }

    public void addForeignKey(dbForeignKey fk)
    {
        this.getForeignKey().add(fk);
    }


    @Override
    public ArrayList<dbConnection> getConnections() {
        ArrayList<dbConnection> connection = new ArrayList<dbConnection>();
        for ( dbForeignKey fk : this.getForeignKey())
           connection.add(fk);
        return connection;
    }

    /**
     * @return the foreignKey
     */
    public ArrayList<dbForeignKey> getForeignKey() {
        return foreignKey;
    }

    @Override
    public String toString()
    {
        return tableName;
    }
}
